package xin.marcher.module.inventory.api.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import xin.marcher.module.common.core.JsonResult;
import xin.marcher.module.common.exception.ProductBizException;
import xin.marcher.module.inventory.api.InventoryServiceApi;
import xin.marcher.module.inventory.domain.request.InventoryRequest;
import xin.marcher.module.inventory.domain.request.WarningInventoryRequest;
import xin.marcher.module.inventory.service.InventoryBucketService;
import xin.marcher.module.inventory.service.InventoryService;

import javax.annotation.Resource;

/**
 * 库存服务
 */
@Slf4j
@DubboService(version = "1.0.0", interfaceClass = InventoryServiceApi.class, retries = 0)
public class InventoryServiceApiImpl implements InventoryServiceApi {

    @Resource
    private InventoryService inventoryService;

    @Resource
    private InventoryBucketService inventoryBucketService;

    /**
     * 库存预警消息通知
     *
     * @param request
     * @return
     */
    @Override
    public JsonResult warningInventoryMessage(WarningInventoryRequest request) {
        try {
            inventoryService.warningInventoryMessage(request);
            return JsonResult.buildSuccess();
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 扣减商品库存
     *
     * @param request
     * @return
     */
    @Override
    public JsonResult deductProductStock(InventoryRequest request) {
        try {
            return inventoryService.deductProductStock(request);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 取消订单 回退商品库存
     *
     * @param request
     * @return
     */
    @Override
    public JsonResult cancelProductStock(InventoryRequest request) {
        try {
            return inventoryService.cancelProductStock(request);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult processBucketOperate() {
        try {
            inventoryBucketService.processBucketOperate();
            return JsonResult.buildSuccess();
        } catch (ProductBizException e) {
            log.error("biz error", e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error", e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult bucketOperateFinishedClear() {
        try {
            // 处理执行成功的
            inventoryBucketService.bucketOperateFinishedClear();
            return JsonResult.buildSuccess();
        } catch (ProductBizException e) {
            log.error("biz error", e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error", e);
            return JsonResult.buildError(e.getMessage());
        }
    }


    @Override
    public JsonResult calculateOfflineThreshold() {
        try {
            // 处理执行成功的
            inventoryBucketService.calculateOfflineThreshold();
            return JsonResult.buildSuccess();
        } catch (ProductBizException e) {
            log.error("biz error", e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error", e);
            return JsonResult.buildError(e.getMessage());
        }
    }
}
